#! /bin/sh

SHM_SIZE=20M
SHM_MODE=755
TTYGRP=5
TTYMODE=620

. /lib/lsb/init-functions

tmpfs_start()
{
	local opt="mode=1777"

	mountpoint -q /tmp && opt="remount,$opt"
	TMPFS_SIZE=20%
	[ -f /etc/sysconfig/tmpfs ] && . /etc/sysconfig/tmpfs
	opt="$opt${TMPFS_SIZE:+,size=$TMPFS_SIZE}"
	[ -d /tmp ] || mkdir /tmp
	mount -t tmpfs -o $opt tmpfs /tmp 2>/dev/null || return 1

	mkdir -p /var/{run,log,lock,misc}
	mountpoint -q /var/run || \
		mount -t tmpfs -o mode=0755,nosuid varrun /var/run
	mountpoint -q /var/lock || \
		mount -t tmpfs -o mode=0755,nosuid,nodev,noexec \
			varlock /var/lock
	return 0
}

sysfs_start()
{
	local opt=""
	mountpoint -q /sys && opt="remount,"

	mount -t sysfs -o ${opt}nodev,noexec,nosuid sysfs /sys 2>/dev/null
}

proc_start()
{
	local opt=""
	mountpoint -q /sys && opt="remount,"

	mount -t proc -o ${opt}nodev,noexec,nosuid proc /proc 2>/dev/null
}

dev_start()
{
	mountpoint -q /dev ||
		mount -t devtmpfs -o mode=0755 devtmpfs /dev 2>/dev/null || {
		mount -t tmpfs -o size=20M,mode=0755 udev /dev
		[ -e /dev/console ] || mknod -m 0600 /dev/console c 5 1
		[ -e /dev/null ] || mknod /dev/null c 1 3
	}

	[ -d /dev/shm ] || mkdir -p --mode=755 /dev/shm || return 1
	mountpoint -q /dev/shm ||
		mount -t tmpfs -o nosuid,nodev,size=$SHM_SIZE,mode=$SHM_MODE \
			shmfs /dev/shm

	[ -d /dev/pts ] || mkdir -p --mode=755 /dev/pts || return 1
	mountpoint -q /dev/pts ||
		mount -t devpts	\
			-o noexec,nosuid,gid=$TTYGRP,mode=$TTYMODE\
			devpts /dev/pts

	return 0
}

do_start()
{
	local x
	: > /etc/mtab
	for x in dev tmpfs sysfs proc; do
		log_info_msg "Mount $x"
		${x}_start && log_success_msg || log_failure_msg
	done
}

case "$1" in
	start) do_start ;;
	stop) ;;
	*) echo "Usage: $0 {start}" >&2 ; exit 1;;
esac

exit 0
